﻿using DevComponents.DotNetBar;
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using zyconfig;

namespace zymain
{
    public partial class FrmUser : Form
    {
        public int uid=0;
        private int[] mids = new int[0];
        private string[] vids = new string[0];
        public FrmUser()
        {
            InitializeComponent();
        }
        private void EditRole()
        {
            string mid = string.Empty;
            using (DbOperHandler _open = new Common().Doh())
            {
                if (!this.uid.Equals(0))
                {

                }
                _open.Reset();
                _open.SqlCmd = " SELECT M_ID,M_TITLE FROM menu WHERE M_PID =0 order by M_SORT";
                DataTable dt = _open.GetDataTable();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TabItem tabItem = new TabItem();
                    tabItem.Name = dt.Rows[i]["M_ID"].ToString();
                    tabItem.Text = dt.Rows[i]["M_TITLE"].ToString();
                    tabItem.Tag = i;
                    TabControlPanel tabControlPanel = new TabControlPanel();
                    tabControlPanel.Dock = DockStyle.Fill;
                    
                    SplitContainer sc = new SplitContainer();
                    sc.Name= "sc"+ dt.Rows[i]["M_ID"].ToString();
                    sc.Tag = "sc" + dt.Rows[i]["M_ID"].ToString();
                    sc.Dock= DockStyle.Fill;
                    sc.Orientation = Orientation.Horizontal;
                    sc.SplitterDistance = 50;
                    sc.Panel1.ResumeLayout(false);
                    sc.Panel2.ResumeLayout(false);
                    ((System.ComponentModel.ISupportInitialize)(sc)).EndInit();
                    sc.ResumeLayout(false);
                    tabControlPanel.Controls.Add(sc);
                    tabItem.AttachedControl = tabControlPanel;
                    tabControlPanel.TabItem = tabItem;
                    this.qxTab.Controls.Add(tabControlPanel);
                    this.qxTab.Tabs.Add(tabItem);
                    _open.Reset();
                    _open.SqlCmd = " SELECT M_ID,M_TITLE,M_PID FROM menu WHERE  M_PID =" + dt.Rows[i]["M_ID"].ToString() + " and M_ISMENU<2 order by M_SORT";
                    DataTable dr = _open.GetDataTable();
                    int w = 40; 
                    for (int j = 0; j < dr.Rows.Count; j++)
                    {
                        CheckBox checkBox = new CheckBox();
                        checkBox.Name = "check" + dr.Rows[j]["M_ID"].ToString();
                        checkBox.Text = dr.Rows[j]["M_TITLE"].ToString();
                        checkBox.Tag= "check" + dr.Rows[j]["M_PID"].ToString();
                        checkBox.TabIndex = Convert.ToInt32(dr.Rows[j]["M_ID"].ToString());
                        checkBox.Size = new Size(120, 24);
                        checkBox.Location = new Point(w, 25);
                        checkBox.CheckedChanged+= new System.EventHandler(this.c_check);
                        w += checkBox.Width;
                        if (mids.Length > 0 && mids.Contains(Convert.ToInt32(dr.Rows[j]["M_ID"].ToString())))
                            checkBox.Checked = true;
                        sc.Panel1.Controls.Add(checkBox);
                    }
                    
                }
            }
        }
        private Object getControlByTag(Object obj, String tagText)
        {
            IEnumerator control = null;//存储当前窗体所有容器喝控件的枚举集合
            Control curr = null;       //循环遍历存放当前控件
            Control result = null;     //控件遍历的查询结果

            //获取当前窗体所有容器和控件的枚举集合
            control = ((Control)obj).Controls.GetEnumerator();
            while (control.MoveNext())
            {
                // 循环进入当前控件
                curr = (Control)control.Current;
                //判断控件是否为包含子控件（是否为容器）
                if (curr.HasChildren)
                {
                    // 是容器则进行递归操作
                    result = (Control)getControlByTag(curr, tagText);
                    if (result == null)
                    {
                        // 说明当前循环（容器）没有 跳出继续查
                        continue;
                    }
                    else
                    {
                        return result;
                    }
                }
                //不是则判断控件tag属性是否相同
                else if ((String)curr.Tag == tagText)
                {
                    //如果是展示控件的Text属性
                    //MessageBox.Show(curr.Text);
                }
            }
            // 该控件不是容器 在当前容器中也没有找到控件 返回空
            return result;
        }
        private void FrmUser_Load(object sender, EventArgs e)
        {
            string mid = string.Empty;
            string vid = string.Empty;
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = " 代码 = " + uid;
                mid = (string)_open.GetField("系统用户", "菜单");
                vid = (string)_open.GetField("系统用户", "权限");
            }
            if (!string.IsNullOrEmpty(mid))
            {
                string[] mds = mid.Split(',');
                mids = new int[mds.Length];
                for (int i = 0; i < mds.Length; i++)
                {
                    mids[i] = Convert.ToInt32(mds[i]);
                }
            }
            if (!string.IsNullOrEmpty(vid))
            {
                string[] mds = vid.Split(',');
                vids = new string[mds.Length];
                for (int i = 0; i < mds.Length; i++)
                {
                    vids[i] = mds[i];
                }
            }
            this.EditRole();
        }
        private void cz_check(object sender, EventArgs e)
        {
            CheckBox c = (CheckBox)sender;
            int id = Convert.ToInt32(c.Name.Replace("check", ""));
            int pid = Convert.ToInt32(c.Tag.ToString().Replace("check", ""));
        }
        private void c_check(object sender, EventArgs e)
        {
            CheckBox c = (CheckBox)sender;
            int pid = Convert.ToInt32(c.Name.Replace("check",""));
            int sid = Convert.ToInt32(c.Tag.ToString().Replace("check", ""));
            object obj =  this.qxTab.Controls.Find("sc"+sid, true).First();
            SplitContainer sc = obj as SplitContainer;
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.SqlCmd = " SELECT M_ID,M_TITLE,M_PID FROM menu WHERE  M_PID =" + pid + " and M_ISMENU=2 order by M_SORT";
                DataTable dm = _open.GetDataTable();
                int zw = 40;
                sc.Panel2.Controls.Clear();
                if (c.Checked)
                {
                    if (mids.Length > 0 && !mids.Contains(pid))
                    {
                        int[] ids = new int[mids.Length + 1];
                        for(int i=0; i< mids.Length;i++)
                        {
                            ids[i] = mids[i];
                        }
                        ids[mids.Length] = pid;
                        mids = ids;
                        string id = string.Join(",", mids);
                        _open.Reset();
                        _open.ConditionExpress = " 代码 = " + uid;
                        _open.AddFieldItem("菜单", id);
                        _open.Update("系统用户");
                    }
                    for (int j = 0; j < dm.Rows.Count; j++)
                    {
                        CheckBox checkBox = new CheckBox();
                        checkBox.Name = "check" + dm.Rows[j]["M_ID"].ToString();
                        checkBox.Text = dm.Rows[j]["M_TITLE"].ToString();
                        checkBox.Tag = "check" + dm.Rows[j]["M_PID"].ToString(); 
                        checkBox.TabIndex = Convert.ToInt32(dm.Rows[j]["M_ID"].ToString());
                        checkBox.Size = new Size(120, 24);
                        checkBox.Location = new Point(zw, 25);
                        checkBox.CheckedChanged += new System.EventHandler(this.cz_check);
                        zw += checkBox.Width;
                        if (vids.Length > 0 && vids.Contains(dm.Rows[j]["M_PID"].ToString()+"-"+ dm.Rows[j]["M_ID"].ToString()))
                            checkBox.Checked = true;
                        sc.Panel2.Controls.Add(checkBox);
                    }
                }
                else
                {
                    mids = mids.Where(val => val != pid).ToArray();
                    string id = string.Join(",", mids);
                    _open.Reset();
                    _open.ConditionExpress = " 代码 = " + uid;
                    _open.AddFieldItem("菜单", id);
                    _open.Update("系统用户");
                    sc.Panel2.Controls.Clear();
                }
            }
        }

        private void qxtab_SelectedTabChanged(object sender, TabStripTabChangedEventArgs e)
        {
            if (e.OldTab != null)
            {
               // MessageBox.Show(e.NewTab.Tag.ToString() + ":" + e.OldTab.Tag.ToString());
            }
            //else
                //MessageBox.Show(e.NewTab.Tag.ToString());
        }
    }
}
